﻿using FarmSmartSleep.Common.Domain.PM.select;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.SelectFile
{
    /// <summary>
    /// 获取分娩难易度
    /// </summary>
    public class ParturitionDifficultyListHandler : IRequestHandler<ParturitionDifficultyListCommon, Result>
    {

        private readonly IBaseRepository<ParturitionDifficulty> ParturitionDifficultyRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="parturitionDifficultyRep"></param>
        public ParturitionDifficultyListHandler(IBaseRepository<ParturitionDifficulty> parturitionDifficultyRep)
        {
            ParturitionDifficultyRep = parturitionDifficultyRep;
        }
        /// <summary>
        /// 处理器
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ParturitionDifficultyListCommon request, CancellationToken cancellationToken)
        {
            var result  =new Result() { Code=(int)CodeEnum.search};
            try
            {
                var list = await ParturitionDifficultyRep.GetAll().ToListAsync();
                if(list.Count != 0)
                {
                    result.Data = list;
                    result.Code = (int)CodeEnum.success;
                }
                else
                {
                    result.Code = (int)CodeEnum.error;
                    result.Messege = "查询异常";
                }
            }
            catch (Exception)
            {
                result.Messege = "获取数据失败";
                throw;
            }
            return result;

        }
    }
}
